﻿using Swift;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EditorManager : MonoBehaviour
{
    public Transform ObjRootInEdit;
    public Transform ObjRootInPlay;
    public Transform UERootInEdit;
    public Transform UERootInPlay;

    public Canvas UIRoot;
    public OpMenu OpMenu;

    bool playing = false;
    public Graph G = new Graph();

    public PowerSource PowerSource { get { return ps; } }
    PowerSource ps = null;
    private void Start()
    {
        var pe = AddUnitAt<PowerSource>("power", Vector2.zero);
        pe.Selected = true;
        ps = pe.Unit as PowerSource;
        ps.AngularVelocity = Mathf.PI;
        playing = false;
    }

    Dictionary<Unit, UnitEditor> ubs = new Dictionary<Unit, UnitEditor>();
    public UnitEditor GetEditor(Unit u)
    {
        return ubs[u];
    }

    // 添加一个部件到场景内
    public UnitEditor AddUnitAt<T>(string name, Vector2 pos) where T : Unit, new()
    {
        var u = UnitFactory.Instance.Create<T>(name);
        u.Pos = pos;
        G.AddUnit(u);

        var go = new GameObject();
        go.name = name;
        go.transform.SetParent(playing ? ObjRootInPlay : ObjRootInEdit, false);
        go.transform.localPosition = new Vector3(pos.x, pos.y, 0);
        go.transform.localRotation = Quaternion.Euler(0, 0, MathEx.Arc2Dir(u.Angle));

        var uiGo = new GameObject();
        uiGo.name = name + "_editor";
        uiGo.transform.SetParent(playing ? UERootInPlay : UERootInEdit, false);

        var e = EditorFactory.Instance.Create<T>(go, uiGo, u);
        e.EMgr = this;

        e.TryLink();
        e.Refresh();
        ubs[u] = e;
        return e;
    }

    // 删除一个部件
    public void DeleteUnit(UnitEditor e)
    {
        G.RemoveUnit(e.Unit);
        ubs.Remove(e.Unit);
        Destroy(e.UB.gameObject);
        Destroy(e.gameObject);
    }

    public float View2WorldScale
    {
        get
        {
            return Camera.main.orthographicSize / (Screen.height / 2);
        }
    }

    // 世界坐标 => 屏幕坐标
    public Vector3 World2View(Vector2 pos)
    {
        var s = View2WorldScale;
        return new Vector3(pos.x / s + Screen.width / 2, pos.y / s + Screen.height / 2, 0);
    }

    // 屏幕坐标 => 世界坐标
    public Vector2 View2World(Vector3 pos)
    {
        var s = View2WorldScale;
        return new Vector2((pos.x - Screen.width / 2) * s, (pos.y - Screen.height / 2) * s);
    }

    // 当前选中的编辑对象
    public UnitEditor CurrentSelected
    {
        get
        {
            return curSel;
        }
        set
        {
            if (curSel == value)
                return;

            if (curSel != null)
                curSel.Selected = false;

            curSel = value;

            if (curSel != null)
                curSel.Selected = true;
        }
    }  UnitEditor curSel;

    public void OnClickSpace()
    {
        CurrentSelected = null;
        CloseOpMenu();
    }

    public void ShowOpMenuAt(Vector3 pos, string[][] ops, Action<string> onOp)
    {
        OpMenu.gameObject.SetActive(true);
        OpMenu.Refresh(ops, (op) => { CloseOpMenu(); onOp(op); });
        OpMenu.transform.position = pos;
    }

    public void CloseOpMenu()
    {
        OpMenu.gameObject.SetActive(false);
    }

    private void Update()
    {
        if (playing)
            PowerSource.Update(Time.deltaTime, ConnectionType.None);
    }

    public void Play(bool play)
    {
        playing = play;
        if (playing)
        {
            ObjRootInPlay.gameObject.SetActive(true);
            UERootInPlay.gameObject.SetActive(true);
        }
        else
        {
            foreach (Transform child in ObjRootInPlay)
                Destroy(child.gameObject);

            ObjRootInPlay.gameObject.SetActive(false);

            foreach (Transform child in UERootInPlay)
                Destroy(child.gameObject);

            UERootInPlay.gameObject.SetActive(false);

            foreach (var ub in ubs.Values)
                ub.Refresh();
        }
    }
}
